package com.laf.manage.controller.user;

import com.laf.common.annotation.HitsAnnotation;
import com.laf.common.core.controller.BaseController;
import com.laf.common.core.domain.R;
import com.laf.common.utils.SecurityUtils;
import com.laf.common.validation.ValidGroup;
import com.laf.manage.domain.dto.DynamicDTO;
import com.laf.manage.domain.dto.PageDTO;
import com.laf.manage.domain.query.DynamicQuery;
import com.laf.manage.domain.vo.DynamicVO;
import com.laf.manage.enums.VerifyStatusEnum;
import com.laf.manage.service.IDynamicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 内容管理Controller
 *
 * @author zian
 * @date 2024-11-09
 */
@Slf4j
@Api(tags = "动态管理管理")
@RestController("userDynamicController")
@RequestMapping("/user/dynamics")
@RequiredArgsConstructor
public class DynamicController extends BaseController {

    private final IDynamicService dynamicService;


    /**
     * 新增动态
     */
    @PostMapping
    @ApiOperation("新增动态")
    public R addDynamic(@RequestBody @Validated DynamicDTO dto) {
        log.info("新增动态：{}", dto);
        dynamicService.addDynamic(dto);
        return R.ok();
    }

    /**
     * 修改动态
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation("修改动态")
    public R updateDynamic(@RequestBody @Validated(ValidGroup.Crud.Update.class) DynamicDTO dto) {
        log.info("修改动态：{}", dto);
        dynamicService.updateDynamic(dto);
        return R.ok();
    }

    /**
     * 删除动态
     * @param ids
     * @return
     */
    @DeleteMapping("{ids}")
    @ApiOperation("删除动态")
    public R deleteDynamic(@PathVariable Long[] ids) {
        log.info("删除动态：{}", (Object) ids);
        dynamicService.delByIds(Arrays.asList(ids));
        return R.ok();
    }


    /**
    * 查询内容管理列表
    */
    @ApiOperation("查询内容管理列表 --> 全部已经审查通过的")
    @GetMapping("/list")
    public R<PageDTO<DynamicVO>> list(DynamicQuery query) {
        log.info("查询内容管理列表：{}", query);
        query.setVerifyStatus(VerifyStatusEnum.VERIFY_PASS.getCode());
        query.setCreateUserId(null);
        PageDTO<DynamicVO> vo =  dynamicService.selectDynamicVOList(query  );
        return R.ok(vo);
    }

    @ApiOperation("查询自己的动态列表")
    @GetMapping("/current/list")
    public R<PageDTO<DynamicVO>> currentList(DynamicQuery query) {
        Long userId = SecurityUtils.getMobileUserId();
        query.setCreateUserId(userId);
        query.setVerifyStatus(query.getVerifyStatus() == null ? VerifyStatusEnum.VERIFY_PASS.getCode() : query.getVerifyStatus() );
        log.info("查询自己的动态列表：{}", query);
        PageDTO<DynamicVO> vo =  dynamicService.selectDynamicVOList(query );
        return R.ok(vo);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取动态详情")
    public R getDetailsById(@PathVariable Long id) {
        log.info("获取动态详情：{}", id);
        DynamicVO vo = dynamicService.getDetailsById(id , false );
        return R.ok(vo);
    }


    @HitsAnnotation // 统计点击量
    @GetMapping("/passed/{id}")
    @ApiOperation("获取已通过的动态详情 - 会更新浏览量")
    public R getPassedDetailsById(@PathVariable Long id) {
        log.info("获取已通过的动态详情：{}", id);
        DynamicVO vo = dynamicService.getPassedDetailsById(id);
        return R.ok(vo);
    }


}
